<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onBeforeMount } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}
const forSys = proxy.$Sys || []

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = reactive({
  agentCity: '',
  agentArea: '',
  // ----
  isSaler: Number(logUser.is_saler) === 1,
  isSalesman: Number(logUser.is_salesman) === 1,
  isExhibition: Number(logUser.is_exhibition) === 1,
  // ----
  forAdm: String(proxy.$Phone) === String(logUser.telphone)
})

/* ---- */
const areas = ref([])
// ----
const items = ref([])

/* ---- */
const getCurrencyInfo = () => {
  // --获取用户代理地区--
  proxy.$Myfun.getJsonData(logUser.topupth, (resJson) => {
    if (Number(resJson.length) > 0) {
      const CData = resJson[0]
      const forProvinceCity = String(CData.province) === String(CData.city) ? CData.province : CData.province + CData.city
      if (CData.district) {
        user.agentCity = CData.district
        user.agentArea = forProvinceCity + CData.district
      } else if (CData.city) {
        user.agentCity = CData.city
        user.agentArea = forProvinceCity
      } else if (CData.province) {
        user.agentCity = CData.province
        user.agentArea = CData.province
      }
    }
  })
  // ----
  areas.value = [{
    pageType: 1,
    show: user.isSaler || user.isSalesman || user.isExhibition || user.forAdm,
    name: '业绩提成',
    page: 'GetCommission',
    title: '业绩提成',
    info: {
      one: '业绩提成',
      two: '回款金额',
      three: '未领提成',
      four: 'personal',
      five: 'first'
    },
    // ----
    tabName: 'proportion_one',
    than: ''
  }, {
    pageType: 2,
    show: user.isSalesman || user.forAdm,
    name: '团队奖金',
    page: 'GetCommission',
    title: '团队奖金',
    info: {
      one: '团队奖金',
      two: '团队回款金额',
      three: '未领奖金',
      four: 'personal',
      five: 'second'
    },
    // ----
    tabName: 'proportion_two',
    than: ''
  }, {
    pageType: 3,
    show: user.isSalesman || user.forAdm,
    name: '管理佣金',
    page: 'GetCommission',
    title: '管理佣金',
    info: {
      one: '管理佣金',
      two: '运营区域回款金额',
      three: '未领佣金',
      four: 'agent',
      five: 'third'
    },
    // ----
    tabName: 'proportion_agent',
    than: '',
    claim: {
      type: 'post',
      url: 'wechat/get-salary-log',
      data: {
        dealer_id: logUser.id,
        district: user.agentCity,
        source: '代理佣金'
      }
    }
  }]
  // ----
  areas.value.forEach((item) => {
    const newItems = forSys.filter((curItem) => { return String(curItem.name) === item.tabName })
    if (Number(newItems.length) === 1) {
      item.than = Math.round(Number(newItems[0].value) * 100) + '%'
    }
    // ----
    items.value.push({
      show: item.show,
      child: [{
        show: true,
        isArrow: true,
        name: item.name,
        callback: () => {
          const forExtend = item.claim || {}
          Object.assign(forExtend, item.info)
          proxy.$Myfun.openPage(proxy, 'push', item.page, { pageType: item.pageType, title: item.title, than: item.than, exTend: JSON.stringify(forExtend) })
        }
      }]
    })
  })
}
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onBeforeMount(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <div class="xw-msg xw-bgCL-11" v-if="user.isSalesman && user.agentArea"><span>代理区域：{{user.agentArea}}</span></div>
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
  </div>
</template>

<style scoped>
/* ---- */
.xw-msg { margin: 0 0 1rem; }
</style>